﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace RTS
{
	public class TrainingUnitsBuilding : Building
	{
		// Representation
		protected float offsetQueueX = 10;
		protected float offsetQueueY = 215;
		protected float separationQueueElements = 4;

		protected Position2D dispatchUnitsPositionOffset = new Position2D(150, 0);

		private const float maxAnimationTimeCounting = 1.0F;
		private const float animationTimeOffset = 0.8F;
		private const float animationSpeed = 0.7F;
		private const int maxUnitsTraining = 5;

		private List<Unit> unitsQueue = new List<Unit>();
		protected TrainOptionsCreator optionsCreator;

		private float elapsedAnimationTime = 0;
		private float elapsedTrainingTime = 0;
		private Position2D dispatchUnitsPosition = new Position2D(150, 0);	// Relative to the building position

		protected RTSTrainingBuildingTexture trainingBuildingTextures;

		public TrainingUnitsBuilding(GameMain game)
			: base(game)
		{

		}

		public override void LoadContent(ContentManager Content)
		{
			buildingTextures = trainingBuildingTextures;
			base.LoadContent(Content);
		}

		public override void setPosition(Position2D pos)
		{
			base.setPosition(pos);
			dispatchUnitsPosition = new Position2D(pos.X + dispatchUnitsPositionOffset.X, pos.Y + dispatchUnitsPositionOffset.Y);
		}

		public override float getTimeElapsed()
		{
			return elapsedAnimationTime;
		}

		public override void update()
		{
			countAnimationTime();

			if (unitsQueue.Count > 0)
			{
				elapsedTrainingTime = elapsedTrainingTime + 0.02F;

				if (unitsQueue[0].getTrainTime() <= elapsedTrainingTime)
				{
					elapsedTrainingTime = 0;

					Unit unit = unitsQueue[0];
					unitsQueue.RemoveAt(0);

					unit.setPosition(dispatchUnitsPosition.clone());
					game.addUnit(unit);
				}
			}

			base.update();
		}

		private void countAnimationTime()
		{
			if (unitsQueue.Count > 0 && unitsQueue[0].getTrainTime() <= animationTimeOffset + elapsedTrainingTime)
			{
				setState(Spawning);
				elapsedAnimationTime = 0;
			}

			elapsedAnimationTime += 0.02F * animationSpeed;

			if (state == Spawning && maxAnimationTimeCounting < elapsedAnimationTime)
			{
				setState(Idle);
				elapsedAnimationTime = 0;
			}
		}

		public List<Unit> getUnitsQueue()
		{
			return unitsQueue;
		}

		public void train(Type unitType)
		{
			Unit unitToQueue = (Unit)(Activator.CreateInstance(unitType, game));
			unitToQueue.LoadContent(Content);
			unitToQueue.assignCamera(camera);

			if (maxUnitsTraining > unitsQueue.Count) unitsQueue.Add(unitToQueue);
		}

		public override void Draw(SpriteBatch spriteBatch)
		{
			base.Draw(spriteBatch);
			if (IsSelected()) drawUnitQueue(spriteBatch);
		}

		private void drawUnitQueue(SpriteBatch spriteBatch)
		{
			TextureSize buildingTextureSize = buildingTextures.TextureSize;

			for (int i = 0; i < unitsQueue.Count; i++)
			{
				Texture2D queueTexture = trainingBuildingTextures.GetQueueTexture(unitsQueue[i].GetType());

				float xPos = pos.X - buildingTextureSize.Width * camera.Scale / 2 + offsetQueueX + i * queueTexture.Width * camera.Scale + i * separationQueueElements - camera.CornerPos.X + graphPositionOffset.X;
				float yPos = pos.Y - buildingTextureSize.Height * camera.Scale / 2 + offsetQueueY - camera.CornerPos.Y + graphPositionOffset.Y;

				Rectangle rectanglePosition = new Rectangle((int)xPos, (int)yPos, (int)(queueTexture.Width * camera.Scale), (int)(queueTexture.Height * camera.Scale));

				spriteBatch.Draw(queueTexture, new Vector2(xPos, yPos), null, Color.White, 0, new Vector2(0, 0), camera.Scale, SpriteEffects.None, 0);
			}

		}

	}
}
